//
//  SCPetMarketVC.m
//  SuperCat
//
//  Created by QG on 2020/3/28.
//  Copyright © 2020 和信金谷. All rights reserved.
//

#import "SCPetMarketVC.h"
#import "SCBaseTableViewCell.h"
#import "SCTPetMarketCell.h"
#import "SCTPetMarketModel.h"
#import "SCTimerTool.h"
#import "NSDate+Helpers.h"
#import "SC_MEASURE.h"

@interface SCPetMarketVC ()<UITableViewDelegate, UITableViewDataSource, SCTPetMarketListCellDelegate, SCTPetMarketBannerCellDelegate>

/** 数据数组 */
@property(nonatomic, strong)NSArray *modelsArray;

/** 计时器 */
@property(nonatomic, strong)SCTimerTool *timer;

/** banner */
@property(nonatomic, weak)UITableView *listView;

@end

@implementation SCPetMarketVC

// - MARK: <-- 初始化 -->
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    UITableView *listView = [UITableView createGrouped:self];
    listView.frame = self.view.bounds;
    listView.h -= kTabBarHeight;
    listView.backgroundColor = RGBHEX(0xC2C2C2);
    SCTPetMarketBannerView *bannerView = [[SCTPetMarketBannerView alloc]initWithFrame:CGRectMake(0, 0, self.view.w, [SCTPetMarketBannerView viweHei])];
    listView.tableHeaderView = bannerView;
    [listView registerClass:[SCTPetMarketListCell class] forCellReuseIdentifier:kSCTPetMarketListCellID];
    [self.view addSubview:listView];
    _listView = listView;
    
    // - 计时器
    SCTimerTool *timer = [[SCTimerTool alloc]init];
    __block NSInteger time = [[NSDate date] timeIntervalSince1970];
    [timer addTimerActionWithTimeInterval:1 repeat:YES callback:^{
        time++;
        bannerView.timeLabel.text = [NSDate dateFormatter:@"yyyy年MM月dd日 HH时mm分ss秒" timestamp:time];
    }];
    [((SCTPetMarketBannerView *)self.listView.tableHeaderView) setBannerModelsArray:[self.modelsArray firstObject]];
}

// - MARK: <-- 懒加载 -->
-(NSArray *)modelsArray{
    if (!_modelsArray) {
        NSMutableArray * listModes = [NSMutableArray array];
        NSMutableArray * bannerModels = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            SCTPetMarketListModel *listModel = [SCTPetMarketListModel new];
            [listModes addObject:listModel];
            SCTPetMarketBannerModel *bannderModel = [SCTPetMarketBannerModel new];
            [bannerModels addObject:bannderModel];
        }
        
        _modelsArray = @[bannerModels, listModes];
    }
    return _modelsArray;
}

// - MARK: <-- 代理 -->
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[self.modelsArray lastObject] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCTPetMarketListModel *model = [self.modelsArray lastObject][indexPath.section];
    return  model.cellHei;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCTPetMarketListCell *cell = [tableView dequeueReusableCellWithIdentifier:kSCTPetMarketListCellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    SCTPetMarketListModel *model = [self.modelsArray lastObject][indexPath.section];
    cell.listModel = model;
    cell.delegate = self;
    return cell;

}

- (void)didSelectListModel:(SCTPetMarketListModel *)model{
    Log(@"didSelectListModel");
}

-(void)didSelectBannderModel:(SCTPetMarketBannerModel *)bannder{
    Log(@"didSelectBannderModel");
}
@end
